﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.ViewModels/VisionOffsetViewModel
* 类       名: VisionOffsetViewModel
* 功      能:  TD
* 时      间:  2024-04-29 17:48:49
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using WaferSelectV3.Common;
using WaferSelectV3.DAL.DataEntity;
using WaferSelectV3.DAL.LocalDataAccess;
using WaferSelectV3.Models;
using WaferSelectV3.Services;

namespace WaferSelectV3.ViewModels
{
    public class VisionOffsetViewModel : BindableBase
    {

        public VisionOffsetViewModel()
        {
            SaveVisionOffsetsCommand = new DelegateCommand(DoSaveVisonOffsets);
            ReloadVisionOffsets();
        }

        public DelegateCommand SaveVisionOffsetsCommand { get; private set; }
        private async void DoSaveVisonOffsets()
        {
            foreach (var visionOffset in VisionOffsetList)
            {
                var entity = new VisionOffsetEntity()
                {
                    Id = visionOffset.Id,
                    ModuleName = visionOffset.ModuleName,
                    OptionName = visionOffset.OptionName,
                    OffsetK = visionOffset.OffsetK,
                    OffsetB = visionOffset.OffsetB,
                };
                var result = VisionOffsetAccess.UpdateDataSingle(entity);
                if (!result.State)
                {
                    OperateRes = "保存失败: " + result.Message;
                    return;
                }
            }
            OperateRes = "保存成功!";
            ReloadVisionOffsets();
            await Task.Delay(2000);
            OperateResShow = "Collapsed";
            OperateRes = "";
        }

        /// <summary>
        /// 保存操作结果
        /// </summary>
        private string? _operateRes;

        public string? OperateRes
        {
            get { return _operateRes; }
            set
            {
                _operateRes = value;
                OperateResShow = "Visible";
                if (value!.Contains("保存成功"))
                {
                    OperateResColor = new SolidColorBrush(Colors.Green);
                }
                else
                {
                    OperateResColor = new SolidColorBrush(Colors.Red);
                }
                RaisePropertyChanged();
            }
        }


        /// <summary>
        /// 保存操作结果的字体颜色,红色表示失败,绿色表示成功
        /// </summary>
        private SolidColorBrush? _operateResColor;

        public SolidColorBrush? OperateResColor
        {
            get { return _operateResColor; }
            set
            {
                _operateResColor = value;
                RaisePropertyChanged();
            }
        }


        private string? _operateResShow = "Collapsed";

        public string? OperateResShow
        {
            get { return _operateResShow; }
            set
            {
                _operateResShow = value;
                RaisePropertyChanged();
            }
        }



        private void ReloadVisionOffsets()
        {
            DataResult<List<VisionOffsetEntity>> result = VisionOffsetAccess.GetVisionOffsets();
            if (result.State)
            {
                Application.Current?.Dispatcher.Invoke(() =>
                {
                    VisionOffsetList.Clear();
                    foreach (var entity in result.Data!)
                    {
                        VisionOffsetModel model = new VisionOffsetModel()
                        {
                            Id = entity.Id,
                            ModuleName = entity.ModuleName,
                            OptionName = entity.OptionName,
                            OffsetK = entity.OffsetK,
                            OffsetB = entity.OffsetB,
                        };
                        VisionOffsetList.Add(model);
                    }
                });

                UpdateVisionOffsetGlobalMap(result.Data!);
            }
            else
            {
                AppCloseProvider.CloseAppWithErrorHappen(result.Message!);
            }
        }

        private ObservableCollection<VisionOffsetModel> _visionOffsetList = new ObservableCollection<VisionOffsetModel>();

        public ObservableCollection<VisionOffsetModel> VisionOffsetList
        {
            get { return _visionOffsetList; }
            set
            {
                _visionOffsetList = value;
                RaisePropertyChanged();
            }
        }



        public static void UpdateVisionOffsetGlobalMap(List<VisionOffsetEntity> visionOffsets)
        {
            foreach (var visionOffset in visionOffsets)
            {
                double[] doubleKb = new double[2] { visionOffset.OffsetK, visionOffset.OffsetB };
                switch (visionOffset.OptionName)
                {
                    case "平均厚度":
                        GlobalValues.VisionOffsetMap["ThicknessAver"] = doubleKb;
                        break;
                    case "TTV":
                        GlobalValues.VisionOffsetMap["TTV"] = doubleKb;
                        break;
                    case "线痕最大值":
                        GlobalValues.VisionOffsetMap["LineTraceMax"] = doubleKb;
                        break;
                    case "翘曲":
                        GlobalValues.VisionOffsetMap["Warping"] = doubleKb;
                        break;
                    case "最小厚度":
                        GlobalValues.VisionOffsetMap["ThicknessMin"] = doubleKb;
                        break;
                    case "最大厚度":
                        GlobalValues.VisionOffsetMap["ThicknessMax"] = doubleKb;
                        break;
                    case "是否密集线痕":
                        GlobalValues.VisionOffsetMap["IsDenseLine"] = doubleKb;
                        break;
                    case "上线痕数量":
                        GlobalValues.VisionOffsetMap["LineTraceUpNum"] = doubleKb;
                        break;
                    case "上线痕数量最大值":
                        GlobalValues.VisionOffsetMap["LineTraceUpMax"] = doubleKb;
                        break;
                    case "下线痕数量":
                        GlobalValues.VisionOffsetMap["LineTraceDownNum"] = doubleKb;
                        break;
                    case "下线痕数量最大值":
                        GlobalValues.VisionOffsetMap["LineTraceDownMax"] = doubleKb;
                        break;
                    case "线痕数量":
                        GlobalValues.VisionOffsetMap["LineTraceCount"] = doubleKb;
                        break;
                    case "边长最大值":
                        GlobalValues.VisionOffsetMap["LengthMax"] = doubleKb;
                        break;
                    case "边长最小值":
                        GlobalValues.VisionOffsetMap["LengthMin"] = doubleKb;
                        break; ;
                    case "X边长":
                        GlobalValues.VisionOffsetMap["LengthX"] = doubleKb;
                        break;
                    case "Y边长":
                        GlobalValues.VisionOffsetMap["LengthY"] = doubleKb;
                        break;
                    case "对角线左上右下":
                        GlobalValues.VisionOffsetMap["DiagonalLeftStart"] = doubleKb;
                        break;
                    case "对角线右上左下":
                        GlobalValues.VisionOffsetMap["DialonalRightStart"] = doubleKb;
                        break;
                    case "倒角最大值":
                        GlobalValues.VisionOffsetMap["ChamferMax"] = doubleKb;
                        break;
                    case "倒角最小值":
                        GlobalValues.VisionOffsetMap["ChamferMin"] = doubleKb;
                        break;
                    case "上下平行度":
                        GlobalValues.VisionOffsetMap["UpDownParallel"] = doubleKb;
                        break;
                    case "左右平行度":
                        GlobalValues.VisionOffsetMap["LeftRightParallel"] = doubleKb;
                        break;
                    case "直角边最大值":
                        GlobalValues.VisionOffsetMap["SquareLineMax"] = doubleKb;
                        break;
                    case "直角边最小值":
                        GlobalValues.VisionOffsetMap["SquareLineMin"] = doubleKb;
                        break;
                    case "垂直度最大值":
                        GlobalValues.VisionOffsetMap["VerticalAngleMax"] = doubleKb;
                        break;
                    case "垂直度最小值":
                        GlobalValues.VisionOffsetMap["VerticalAngleMin"] = doubleKb;
                        break;
                    case "左上倒角":
                        GlobalValues.VisionOffsetMap["LeftTopCharmfer"] = doubleKb;
                        break;
                    case "右上倒角":
                        GlobalValues.VisionOffsetMap["RightTopCharmfer"] = doubleKb;
                        break;
                    case "左下倒角":
                        GlobalValues.VisionOffsetMap["LeftBottomCharmfer"] = doubleKb;
                        break;
                    case "右下倒角":
                        GlobalValues.VisionOffsetMap["RightBottomCharmfer"] = doubleKb;
                        break;
                    case "左上角度":
                        GlobalValues.VisionOffsetMap["LeftTopAngle"] = doubleKb;
                        break;
                    case "右上角度":
                        GlobalValues.VisionOffsetMap["RightTopAngle"] = doubleKb;
                        break;
                    case "左下角度":
                        GlobalValues.VisionOffsetMap["LeftBottomAngle"] = doubleKb;
                        break;
                    case "右下角度":
                        GlobalValues.VisionOffsetMap["RightBottomAngle"] = doubleKb;
                        break;
                    case "X边长最大值":
                        GlobalValues.VisionOffsetMap["LengthXMax"] = doubleKb;
                        break;
                    case "X边长最小值":
                        GlobalValues.VisionOffsetMap["LengthXMin"] = doubleKb;
                        break;
                    case "Y边长最大值":
                        GlobalValues.VisionOffsetMap["LengthYMax"] = doubleKb;
                        break;
                    case "Y边长最小值":
                        GlobalValues.VisionOffsetMap["LengthYMin"] = doubleKb;
                        break;
                    case "电阻率值":
                        GlobalValues.VisionOffsetMap["NaponsNc"] = doubleKb;
                        break;
                }
            }
        }
    }
}
